using System;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Library.WebControls.SimpleTab {
	[ToolboxBitmap(typeof(SpTabControl), "Library.WebControls.SimpleTab.SpTabControl.bmp")]
	[Designer(typeof(SpTabControlDesign))]
	[SupportsEventValidation]
	[DefaultProperty("Tabs")]
	[DesignTimeVisible(true)]
	[ToolboxData("<{0}:SpTabControl runat=server></{0}:SpTabControl>")]
	public class SpTabControl : BaseAjaxControl {
		private static readonly Type SpTabControlType = typeof(SpTabControl);



		private HtmlGenericControl _tab;
		private SpTabPageCollection _tabs;

		public SpTabControl() {
			CssFileName = BaseCssDir + "sptab-ui.css";
			ClientScript = new ClientScript();
		}

		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[DefaultValue("")]
		public ClientScript ClientScript { private set; get; }
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[DefaultValue("")]
		public SpTabPageCollection Tabs {
			get {
				EnsureChildControls();
				return _tabs;
			}
		}

		private HtmlGenericControl _divTitleContent;
		internal HtmlGenericControl DivTitleContent {
			get {
				EnsureChildControls();
				return _divTitleContent;
			}
		}


		public int SelectedIndex { get; set; }

		internal HtmlGenericControl Tab {
			get {
				EnsureChildControls();
				return _tab;
			}
		}
		public override string ID {
			get {
				EnsureChildControls();
				return _tab.ID;
			}
			set {
				EnsureChildControls();
				_tab.ID = value;
			}
		}
		public override string ClientID {
			get {
				EnsureChildControls();
				return _tab.ClientID;
			}
		}
		public Unit Width {
			get {
				EnsureChildControls();
				return new Unit(_tab.Style[HtmlTextWriterStyle.Width]);
			}
			set {
				EnsureChildControls();
				_tab.Style[HtmlTextWriterStyle.Width] = value.ToString();
			}
		}
		[DefaultValue(false)]
		public bool ShowCloseButton { get; set; }
		protected override void CreateChildControls() {
			_tab = new HtmlGenericNaming("div");
			_tab.Attributes["class"] = "spTabControl";
			_tabs = new SpTabPageCollection(this);
			var divTitle = new HtmlGenericControl("div");
			divTitle.Attributes["class"] = "spTabTitle";
			_tab.Controls.Add(divTitle);

			var divTitle1 = new HtmlGenericControl("div");
			divTitle1.Attributes["class"] = "spTabTitle1";
			divTitle.Controls.Add(divTitle1);

			var divTitle2 = new HtmlGenericControl("div");
			divTitle2.Attributes["class"] = "spTabTitle2";
			divTitle1.Controls.Add(divTitle2);

			var divTitle3 = new HtmlGenericControl("div");
			divTitle3.Attributes["class"] = "spTabTitle3";
			divTitle2.Controls.Add(divTitle3);

			_divTitleContent = new HtmlGenericControl("div");
			_divTitleContent.Attributes["class"] = "spTabTitleContent";
			divTitle3.Controls.Add(_divTitleContent);
			Controls.Add(_tab);
		}



		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);
			IncludeScriptOnLoad(SpTabControlType, "SpTab", "Library.WebControls.SimpleTab.include.SpTabControl.js", UtilLib);
		}

		protected override void CreateClientObjectAtRender(HtmlTextWriter writer) {
			BeginScriptTag(writer);
			writer.Write("var {0}=new SpTabControl();", ClientName);
			//var tabsBuilder = new StringBuilder();

			//bool firstTab = true;
			//foreach (var tab in Tabs) {
			//  if (!tab.Visible) continue;
			//  if (firstTab) {
			//    tabsBuilder.Append("[");
			//    firstTab = false;
			//  } else {
			//    tabsBuilder.Append(",");
			//  }
			//  tabsBuilder.AppendFormat("['{0}','{1}','{2}']", tab.TabContain.ClientID, tab.Name, tab.TabMenu.ClientID);
			//}
			//if (tabsBuilder.Length > 0) tabsBuilder.Append("]");


			writer.Write("{0}.Init('{0}',{1},'{2}',{3});", ClientName, ShowCloseButton ? "1" : "0", ClientID, ClientEnable.ToString().ToLower());
			AddScriptHandler(writer, 1, ClientScript.TabActiveChanged, "AddHandler");
			AddScriptHandler(writer, 2, ClientScript.TabActiveChanging, "AddHandler");

			EndScriptTag(writer);
		}
		public override bool ClientVisible {
			get {
				EnsureChildControls();

				return _tab.Style[HtmlTextWriterStyle.Display] != "none";
			}
			set {
				EnsureChildControls();
				if (value) {
					_tab.Style.Remove(HtmlTextWriterStyle.Display);
				} else {
					_tab.Style[HtmlTextWriterStyle.Display] = "none";
				}
			}
		}

		//private List<SpTabPage> _renderVisibleTabs;
		protected override void Render(HtmlTextWriter writer) {
			if (!Visible) return;

			bool hasActiveTab = false;
			int firstVisibleIndex = -1;
			int i = -1;
			for (int j = 0; j < Tabs.Count; j++) {
				var tab = Tabs[j];
				if (!tab.Visible) continue;
				if (firstVisibleIndex == -1) {
					if (tab.Visible) firstVisibleIndex = j;
				}
				i++;
				if (String.IsNullOrEmpty(tab.TabMenu.Name)) {
					tab.Name = "Tab" + i;
				}
				//tab.TabMenu.ID = "tblMenu" + i;
				tab.TabContain.Attributes["_tabname"] = tab.Name;
				tab.TabMenu.HRef = "javascript:void(0)";
				tab.TabMenu.Attributes["onclick"] = ClientName + ".SelectTab(this)";
				if (SelectedIndex == i) {
					tab.TabMenu.Attributes["class"] = "active";
					hasActiveTab = true;
				} else {
					tab.TabContain.Style.Add(HtmlTextWriterStyle.Display, "none");
				}
			}
			if (!hasActiveTab) {
				if (firstVisibleIndex == -1) SelectedIndex = -1;
				else {
					var tab = Tabs[firstVisibleIndex];
					tab.TabMenu.Attributes["class"] = "active";
					tab.TabContain.Style.Remove(HtmlTextWriterStyle.Display);
				}
			}



			base.Render(writer);
		}
	}
}